var Sequelize = require('sequelize');
var sequelize = new Sequelize('broadcastBD', 'root', 'root', {
	host : '127.0.0.1',
	port : '3306', 
	dialect : 'mysql',
	pool: {
	 	maxConnections: 50,
	 	minConnections: 0,
	 	maxIdleTime: 200
	},
	timezone: '+08:00'
});

var admin = sequelize.define('admin', {
	username: Sequelize.STRING,
	password: Sequelize.STRING,
	typecode: Sequelize.INTEGER
});
var user = sequelize.define('user', {
	username: Sequelize.STRING,
	password: Sequelize.STRING
});
var type = sequelize.define('type', {
	typecode: Sequelize.INTEGER,
	typename: Sequelize.STRING,
	remark: Sequelize.STRING
},{
	timestamps: false
});
var usertype = sequelize.define('typeofuser', {
	userId: Sequelize.INTEGER, 
	typeId: Sequelize.INTEGER
});
var userinfo = sequelize.define('basicinfo', {
	realname: Sequelize.STRING,
	gender: Sequelize.INTEGER,
	idcardnum: Sequelize.STRING,
	cellphone: Sequelize.INTEGER,
	email: Sequelize.STRING,
	area: Sequelize.STRING
});
user.belongsToMany( type , {
	through: {
		model: usertype,
		unique: true
	},
	foreignKey: 'userId'
});
type.belongsToMany( user , {
	through: {
		model: usertype,
		unique: true
	},
	foreignKey: 'typeId'
});

userinfo.belongsTo(user);
user.hasOne(userinfo);

sequelize.sync();
/* type.sync();
 user.sync();
 userinfo.sync();
 usertype.sync();
 admin.sync();*/

exports.user = user;
exports.type = type;
exports.usertype = usertype;
exports.admin = admin;
exports.userinfo = userinfo;